package com.fyyg.wechat.util.userdefinedtag;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import org.apache.commons.lang3.StringUtils;

import com.fyyg.wechat.util.TokenInterceptor;


/**
 * @author PengYu
 * @date 2016年3月25日 下午1:58:21
 */
public class TokenTag extends SimpleTagSupport {

	@Override
	public void doTag() throws JspException, IOException {
		PageContext context= (PageContext)this.getJspContext();
	    //获取Token
		HttpSession session=context.getSession();
	    String token=(String) session.getAttribute(TokenInterceptor.TOKEN_SESSION);
	    if(StringUtils.isEmpty(token)){
	    	token =TokenInterceptor.createToken((HttpServletRequest)context.getRequest());
	    }
		getJspContext().getOut().println(" <input type=\"hidden\" name=\"token\"  value=\""+token.replace('-', '_')+"\" id=\"token\"/> ");
	}
	
}
